package com.szqhyxj.youxiangju;

import android.app.Dialog;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.szqhyxj.youxiangju.sys.util.ToastUtils;
import com.umeng.analytics.MobclickAgent;

public class BaseActivity extends AppCompatActivity implements PromptView{

    private Dialog dialog;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState){
        super.onCreate(savedInstanceState);

        //禁止横屏
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
    }

    @Override
    public void showToast(String msg){
        ToastUtils.showTost(this,msg);
    }

    @Override
    public void showLoading(String msg) {
        if (dialog != null && dialog.isShowing()) return;
        dialog = new Dialog(this, R.style.Dialog);
        View view = getLayoutInflater().inflate(R.layout.pro_dialog_toash, null);

        TextView txt = (TextView) view.findViewById(R.id.txt_content);
        txt.setText(msg);

        dialog.addContentView(view, new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
        dialog.setCanceledOnTouchOutside(false);
        dialog.show();
    }

    @Override
    public void showLoading() {
        if (dialog != null && dialog.isShowing()) return;
        dialog = new Dialog(this, R.style.Dialog);
        View view = getLayoutInflater().inflate(R.layout.pro_dialog_toash, null);

        TextView txt =  view.findViewById(R.id.txt_content);
        txt.setText("加载中..");

        dialog.addContentView(view, new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
        dialog.setCanceledOnTouchOutside(false);
        dialog.show();
    }

    @Override
    public void dismissLoading() {
        if (dialog != null && dialog.isShowing()) {
            dialog.dismiss();
            dialog = null;
        }
    }

    @Override
    protected void onDestroy(){
        super.onDestroy();
        dismissLoading();
    }

    @Override
    protected void onResume() {
        super.onResume();
        MobclickAgent.onResume(this);
    }
    @Override
    protected void onPause() {
        super.onPause();
        MobclickAgent.onPause(this);
    }

}
